package oop;

/**
 * Created with IntelliJ IDEA.
 * ClassName: RecursionTest
 * Package: oop
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-11-19
 * Time: 19:28
 */

//递归方法
public class RecursionTest {

    public static void main(String[] args) {
        RecursionTest test = new RecursionTest();
        //无限递归 栈溢出的操作
        //test.method1();

        int sum = test.getSum(100);
        System.out.println(sum);


        //递归的方法实现
        System.out.println(test.getSum1(100));
        //n!
        System.out.println(test.getMul(5));

        int m = 10;
        System.out.println(test.f(m));

    }

    /**
     * 汉诺塔游戏
     *
     * 斐波那契数列 1 1 2 3 5 8 13 21 34 55
     * f(n) = f(n-1)+f(n-2)
     */
    public int f(int n) {
        if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 1;
        } else {
            return f(n - 1) + f(n - 2);
        }
    }

    /**
     * 计算n的阶乘
     */
    public int getMul(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n * getMul(n - 1);
        }
    }

    /**
     * 计算1-100自然数的总和
     */
    public int getSum1(int num) {
        //对于递归来说 计算getSum(99) + 100 => getSum(99) = getSum(98) + 99
        if (num == 1) {
            //要有一个终止的条件
            return 1;
        } else {
            return getSum1(num - 1) + num;
        }
    }

    public int getSum(int num) {
        int sum = 0;
        for (int i = 1; i <= num; i++) {
            sum += i;
        }
        return sum;
    }

    public void method1() {
        System.out.println("method1.........");
        method1();
    }

}
